package com.dhcc.bpm.modules.content.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dhcc.bpm.common.annotation.Log;
import com.dhcc.bpm.common.enums.BusinessType;
import com.dhcc.bpm.common.utils.SafeHtmlUtils;
import com.dhcc.bpm.modules.common.entity.Plupload;
import com.dhcc.bpm.modules.common.service.IPluploadService;
import com.dhcc.bpm.modules.content.entity.News;
import com.dhcc.bpm.modules.content.service.INewsService;
import com.dhcc.bpm.modules.content.vo.NewsVO;
import com.dhcc.bpm.modules.content.vo.NewsVO2;
import com.dhcc.bpm.modules.system.service.ISerialNumberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.log.annotation.ApiLog;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.Func;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @ClassName NewsController
 * @Description  新闻动态管理 控制器
 * @Author lijiacheng
 * @Date 2020/2/21 14:16
 * @Version V1.0
 * @Copyright: 2020 东华软件 . All rights reserved.
 */

@RestController
@AllArgsConstructor
@RequestMapping("bpm-content/news")
@Api(value = "新闻动态表", tags = "新闻动态表接口")
public class NewsController extends BladeController {

	private INewsService newsService;

	private IPluploadService pluploadService;

	private ISerialNumberService serialNumberService;

	/**
	 * 详情
	 */
	@ApiLog("新闻信息模块表详情")
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入news")
	public R<News> detail(News news) {
		News detail = newsService.getOne(Condition.getQueryWrapper(news));
		Plupload plupload = pluploadService.getById(detail.getPicturePath());
		NewsVO newsVO = new NewsVO();
		BeanUtil.copy(detail, newsVO);
		if(plupload != null){
			newsVO.setFileFullPath(plupload.getFileFullpath());
			newsVO.setStorageType(plupload.getStorageType());
			newsVO.setFileRealname(plupload.getFileRealname());
		}
		return R.data(newsVO);
	}

	/**
	 * 分页 新闻动态表
	 */
	@ApiLog("新闻信息模块表列表")
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入news")
	public R<IPage<NewsVO2>> list(NewsVO2 news, Query query) {
		IPage<NewsVO2> pages = newsService.selectNewsPage2(Condition.getPage(query), news);
		return R.data(pages);
	}

	/**
	 * 自定义分页 新闻动态表
	 */
	@ApiLog("新闻信息模块表分页")
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入news")
	public R<IPage<NewsVO>> page(NewsVO news, Query query) {
		IPage<NewsVO> pages = newsService.selectNewsPage(Condition.getPage(query), news);
		return R.data(pages);
	}

	/**
	 * 新增 新闻动态表
	 */
	@ApiLog("新闻信息模块表新增")
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入news")
	@Log(title = "新闻信息新增", businessType = BusinessType.INSERT)
	public R save(@Valid @RequestBody News news) {
		String safeHtml = SafeHtmlUtils.getSafeHtml(news.getNewsContent());
		news.setNewsContent(safeHtml);
		return R.status(newsService.save(news));
	}

	/**
	 * 修改 新闻动态表
	 */
	@ApiLog("新闻信息模块表更新")
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入news")
	@Log(title = "新闻信息修改", businessType = BusinessType.UPDATE)
	public R update(@Valid @RequestBody News news) {
		String safeHtml = SafeHtmlUtils.getSafeHtml(news.getNewsContent());
		news.setNewsContent(safeHtml);
		return R.status(newsService.updateById(news));
	}

	/**
	 * 新增或修改 新闻动态表
	 */
	@ApiLog("新闻信息模块表提交")
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入news")
	@Log(title = "新闻信息", businessType = BusinessType.INSERT)
	public R submit(@Valid @RequestBody News news) {
		if (news.getId() == null || news.getId() < 0){
			// 设置SDK编号
			String code = serialNumberService.generateSerialNumberByModelCode("opc_news");
			news.setNewsCode(code);
		}
		if( news.getNewsContent()!=null ){
			String safeHtml = SafeHtmlUtils.getSafeHtml(news.getNewsContent());
			news.setNewsContent(safeHtml);
		}
		return R.status(newsService.saveOrUpdate(news));
	}

	
	/**
	 * 删除 新闻动态表
	 */
	@ApiLog("新闻信息模块表删除")
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	@Log(title = "新闻信息删除", businessType = BusinessType.DELETE)
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(newsService.deleteLogic(Func.toLongList(ids)));
	}


	@PostMapping("/deleteImage")
	public R deleteImage(Long id, Long busId){
		if(pluploadService.selectById(busId) != null){
			pluploadService.fileDelete(busId);
		}
		News news = new News();
		news.setId(id);
		news.setPicturePath(-1L);
		return R.status(newsService.updateById(news));
	}

	
}
